﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Mimoza.PuttyPlugin
{
    public class PuttyFile
    {
        public PuttyFile()
        {
        }

        public string GetText(PuttyData data, Guid policyId)
        {
            return FillTemplate(data, policyId);
        }

        public void CreateFile(string fullFileName, PuttyData data, Guid policyId)
        {
            string fileText = FillTemplate(data, policyId);

            Directory.CreateDirectory(Path.GetDirectoryName(fullFileName));

            using (StreamWriter writer = File.CreateText(fullFileName))
            {
                writer.Write(fileText);
                writer.Flush();
            }
        }

        public void CreateShortcutFile(string fullFileName, PuttyData data, Guid policyId)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(fullFileName));

            string cmdLine = "putty.exe " + "@" + GetPolicyIdAndStartupFlag(data, policyId);

            cmdLine = cmdLine.Length.ToString() + "#" + cmdLine;

            using (StreamWriter writer = File.CreateText(fullFileName))
            {
                writer.Write(cmdLine);
                writer.Flush();
            }
        }

        private string FillTemplate(PuttyData data, Guid policyId)
        {
            string text = Resource.PuttyFileTemplate;

            text = text.Replace("%COMP%", data.Computer);
            text = text.Replace("%PORT%", data.Port.ToString());

            text = text.Replace("%SESSIONNAME%", data.SessionName);

            string protoName = "ssh";
            if (data.Type == Type.Telnet) protoName = "telnet";
            text = text.Replace("%PROTO%", protoName);

            text = text.Replace("%USER%", data.UserName);

            text = text.Replace("%WIDTH%", data.WindowSize.Columns.ToString());
            text = text.Replace("%HEIGHT%", data.WindowSize.Rows.ToString());

            text = text.Replace("%SCROLLBACK%", data.Scrollback.ScrollbarLines.ToString());
            int valShow = data.Scrollback.IsShowScrollbarFullScreen ? 1 : 0;
            text = text.Replace("%SCROLLBACK_FULL%", valShow.ToString());
            
            text = text.Replace("%POLICYID%", policyId.ToString());

            text = text.Replace("%POLICYID-STARTUP_FLAG%", GetPolicyIdAndStartupFlag(data, policyId));

            int val = data.IsStartOnStartup ? 1 : 0;
            text = text.Replace("%STARTUPFLAG%", val.ToString() );

            return text;
        }

        public static string GetPolicyIdAndStartupFlag(PuttyData data, Guid policyId)
        {
            int val = data.IsStartOnStartup ? 1 : 0;
            return policyId.ToString() + Mimoza.RdpPlugin.PluginFileSync.FilesPolicyStartupDelimer + 
                val.ToString();
        }

        private string GetYesNo(bool value)
        {
            if (value) return "Yes";
            else return "No";
        }

        private string GetOnOff(bool value)
        {
            if (value) return "On";
            else return "Off";
        }    
    }
}
